{extend name="layout/default" /}
{block name="title"}软件新增{/block}
{block name="content"}
<form class="layui-form" action="">
    <div class="mainBox" style="background-color: white;">
        <div class="main-container">
            <div class="main-container">
                <div class="layui-form-item">
                    <label class="layui-form-label">软件名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="name" lay-verify="title" autocomplete="off" placeholder="请输入软件名称" class="layui-input">
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">更新配置</label>
                    <div class="layui-input-block">
                        <blockquote class="layui-elem-quote">
                            1.自定义更新服务端返回配置，参照 EC 热更新格式，可在系统配置修改默认字段。
                            <br>2.单选框值仅支持 0 和 1，也就是只能有两个结果, 暂时无法扩展
                            <br>3.键名只能为 字母/数字/下划线 组成，且不能以数字开头
                        </blockquote>
                        <div id="view-config"></div>
                        <div class="layui-col-xs12">
                            <button class="pear-btn pear-btn-primary " id="configPlus"> <i class="layui-icon layui-icon-addition"></i> 追加 </button>
                        </div>
                    </div>
                </div>
                <div class="layui-form-item">
                    <label class="layui-form-label">状态</label>
                    <div class="layui-input-block">
                        <input type="radio" name="status" value="0" title="正常" checked>
                        <input type="radio" name="status" value="1" title="禁用">
                    </div>
                </div>
            </div>
        </div>
    </div>
    <div class="bottom">
        <div class="button-container">
            <button type="submit" class="pear-btn pear-btn-primary pear-btn-sm" lay-submit="" lay-filter="form-save">
                <i class="layui-icon layui-icon-ok"></i>
                提交
            </button>
            <button type="reset" class="pear-btn pear-btn-sm">
                <i class="layui-icon layui-icon-refresh"></i>
                重置
            </button>
        </div>
    </div>
</form>

<script type="text/html" id="config-template">
    {{#  layui.each(d, function(index, item){ }}
    <div class="layui-row layui-col-space5">
        <div class="layui-col-xs6 layui-col-sm3">
            <input type="text" name="config[{{ index }}][cnname]" hover placeholder="描述" value="{{ item.cnname }}" class="layui-input">
        </div>
        <div class="layui-col-xs6 layui-col-sm3">
            <input type="text" name="config[{{ index }}][enname]" hover placeholder="键名" value="{{ item.enname }}" class="layui-input">
        </div>
        <div class="layui-col-xs6 layui-col-sm3">
            <select name="config[{{ index }}][type]">
                <option value="">请选择类型</option>
                <option value="file" {{ (item.type=="file") ? "selected" : "" }}>上传</option>
                <option value="text" {{ (item.type=="text") ? "selected" : "" }}>文本</option>
                <option value="textarea" {{ (item.type=="textarea") ? "selected" : "" }}>文本域</option>
                <option value="radio" {{ (item.type=="radio") ? "selected" : "" }}>单选</option>
            </select>
        </div>
        <div class="layui-col-xs2">
            <button class="pear-btn pear-btn-danger " id="configRemove" data-index="{{ index }}"> <i class="layui-icon layui-icon-close"></i> </button>
        </div>
    </div>
    {{#  }); }}
</script>
{/block}
{block name="script"}
<script>
    layui.use(['form','jquery', 'laytpl', 'popup'],function(){
        let form = layui.form;
        let $ = layui.jquery;
        let laytpl = layui.laytpl;
        let popup = layui.popup;
        let MODULE_PATH = "/{:request()->controller()}";

        form.on('submit(form-save)', function(data){
            let roleIds = "";
            $('input[type=checkbox]:checked').each(function() {
                roleIds += $(this).val()+",";
            });
            roleIds = roleIds.substr(0,roleIds.length-1);
            data.field.roleIds = roleIds;
            let loading = layer.load();
            $.ajax({
                url: MODULE_PATH + "/add",
                data: data.field,
                dataType:'json',
                type:'post',
                success:function(res){
                    layer.close(loading);
                    if(Number(res.code) === 0){
                        popup.success(res.msg, function() {
                            parent.layer.close(parent.layer.getFrameIndex(window.name));//关闭当前页
                            parent.layui.table.reload("dataTable");
                        });
                    }else{
                        layer.close(loading);
                        popup.failure(res.msg);
                    }
                },
                error: function (res) {
                    layer.close(loading);
                    popup.failure("网络错误, 请重试");
                }
            })
            return false;
        });

        let configForm = {php}
            $default_config = get_config('software', 'default');
            if(!empty($default_config)) {
                echo json_encode($default_config);
            }
        {/php};

        function setConfinForm() {
            let configTemplate = document.getElementById('config-template').innerHTML,
                viewConfig = document.getElementById('view-config');
            laytpl(configTemplate).render(configForm, function(html){
                viewConfig.innerHTML = html;
                form.render('select');
            });
        }

        // 同步软件配置表单数据
        function syncConfigForm() {
            for (let i = 0, len = configForm.length; i < len; i++) {
                configForm[i]['cnname'] = $("[name='config["+i+"][cnname]']").val();
                configForm[i]['enname'] = $("[name='config["+i+"][enname]']").val();
                configForm[i]['type'] = $("[name='config["+i+"][type]']").val();
            }
        }
        
        // 初始化配置表单
        setConfinForm();

        $(document).on('click', '#configPlus', function () {
            syncConfigForm();
            configForm.push({
                cnname: "",
                enname: "",
                type: "text"
            })
            setConfinForm();
            return false;
        });

        $(document).on('click', '#configRemove', function () {
            syncConfigForm();
            let index = $(this).data('index');
            configForm.splice(index, 1);
            setConfinForm();
            return false;
        })

        let tp_debug = $("#think_page_trace_open");
        (tp_debug.length > 0) && tp_debug.remove();
    })
</script>
{/block}